/**
 *  获取当前日期，结果格式：YYYY-mm-DD
 * */
function getYMD(d) {
    if (d == undefined) {
        d = new Date();
    }
    var year = d.getFullYear();
    var month = d.getMonth() + 1; // 记得当前月是要+1的
    month = month < 10 ? ("0" + month) : month;
    var dt = d.getDate();
    dt = dt < 10 ? ("0" + dt) : dt;
    var today = year + "-" + month + "-" + dt;
    return today;
}

function getTMD(date, format) {
    if (!date) {
        date = new Date();
    }
    if (!format) {
        format = "YYYY-MM-DD";
    }
    var year = d.getFullYear();
    var month = d.getMonth() + 1; // 记得当前月是要+1的
    month = month < 10 ? ("0" + month) : month;
    var dt = d.getDate();
    dt = dt < 10 ? ("0" + dt) : dt;
    var today = year + "-" + month + "-" + dt;
    return today;
}

/**
 *  从date类型转化为string类型，结果格式：YYYY-mm-DD
 * */
function dateTOString(date) {
    var source = new Date(date);
    var yy = source.getFullYear().toString();
    var mm = source.getMonth() + 1;
    mm = mm < 10 ? ('0' + mm.toString()) : mm.toString();
    var dd = source.getDate();
    dd = dd < 10 ? ('0' + dd.toString()) : dd.toString();
    var aa = yy + '-' + mm + '-' + dd;
    return aa;
}

/**
 *  从dateTime转化为string类型，结果格式：YYYY-mm-DD HH:MM:SS
 * */
function dateTimeTOString(dateTime) {
    var source = new Date(dateTime);
    var yy = source.getFullYear().toString();
    var mm = source.getMonth() + 1;
    mm = mm < 10 ? ('0' + mm.toString()) : mm.toString();
    var dd = source.getDate();
    dd = dd < 10 ? ('0' + dd.toString()) : dd.toString();
    var hh = source.getHours();
    hh = hh < 10 ? ('0' + hh.toString()) : hh.toString();
    var MM = source.getMinutes();
    MM = MM < 10 ? ('0' + MM.toString()) : MM.toString();
    var ss = source.getSeconds();
    ss = ss < 10 ? ('0' + ss.toString()) : ss.toString();
    var aa = yy + '-' + mm + '-' + dd + ' ' + hh + ':' + MM + ':' + ss;
    return aa;
}


/**
 *  获取当前时间，结果格式：YYYY-mm-DD HH:MM
 * */
function getYMDHM() {
    var d = new Date();
    var year = d.getFullYear();
    var month = d.getMonth() + 1; // 记得当前月是要+1的
    month = month < 10 ? ("0" + month) : month;
    var dt = d.getDate();
    dt = dt < 10 ? ("0" + dt) : dt;
    var hour = d.getHours();
    hour = hour < 10 ? ("0" + hour) : hour;
    var minu = d.getMinutes();
    minu = minu < 10 ? ("0" + minu) : minu;
    var now = year + "-" + month + "-" + dt + " " + hour + ":" + minu;
    return now;
}
/**
 * 获取服务器当前时间（即获取登录时缓存在session里面的服务器时间）
 */
function getServerTime() {
    let loginInfo = JSON.parse(sessionStorage.getItem("loginInfo"));
    let stateTime = loginInfo.stateTime;
    return stateTime;
}
/**
 * 获取服务器当前时间，格式"YYYY-MM-DD"
 */
function getServerDate() {
    let loginInfo = JSON.parse(sessionStorage.getItem("loginInfo"));
    let stateTime = loginInfo.stateTime;
    let servedate = new Date(stateTime);
    let y, m, d;
    y = servedate.getFullYear();
    m = (servedate.getMonth() + 1) >= 10 ? (servedate.getMonth() + 1) : "0" + (servedate.getMonth() + 1);
    d = servedate.getDate();
    return y + "-" + m + "-" + d;
    return servedate;
}
/**
 * 根据给定的日期和增加/减少的年月日，获取新日期，
 * 例如 getNewDate("2019-03-31","+1",1,2) 就是计算 2019-03-31 的一年零一月零2天后的日期
 * @param {*} date 指定日期，如不指定则默认以登录时获取的服务器时间为准
 * @param {*} yadd 增加/减少的年数，以+/-号作为增加减少标志，不带+/-号时判为增加
 * @param {*} madd 增加/减少的月数，以+/-号作为增加减少标志，不带+/-号时判为增加
 * @param {*} dadd 增加/减少的天数，以+/-号作为增加减少标志，不带+/-号时判为增加
 */
function getNewDate(date, yadd, madd, dadd) {
    let today;
    if (date == undefined) {
        today = getServerDate();
    } else {
        today = new Date(date);
    }
    yadd = yadd != undefined ? Number(yadd) : 0;
    madd = madd != undefined ? Number(madd) : 0;
    dadd = dadd != undefined ? Number(dadd) : 0;
    today.setFullYear(today.getFullYear() + yadd);
    today.setMonth(today.getMonth() + madd);
    today.setDate(today.getDate() + dadd);
    let y, m, d;
    y = today.getFullYear();
    m = (today.getMonth() + 1) >= 10 ? (today.getMonth() + 1) : "0" + (today.getMonth() + 1);
    d = today.getDate();
    return y + "-" + m + "-" + d;
}

export var timeutil = {
    getYMD: getYMD,
    getTMD: getTMD,
    dateTOString: dateTOString,
    dateTimeTOString: dateTimeTOString,
    getYMDHM: getYMDHM,
    getServerTime: getServerTime,
    getServerDate: getServerDate,
    getNewDate: getNewDate,
}